package us.lithub.ui.activity;

import us.lithub.R;
import us.lithub.Util;
import us.lithub.data.GlobalDb;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.fedorvlasov.lazylist.ImageLoader;
/**
 * The Class Settings is the activity for the options screen
 */
public class Settings extends Activity {
	
	private Context mContext = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
	}

	/**
	 * Register or login.
	 *
	 * @param view the view
	 */
	public void register(View view) {
		if (!Util.isRegistered(mContext)) {
			startActivity(new Intent(this, Register.class));
		} else {
			Toast.makeText(this, R.string.already_registered, Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * Clear cache.
	 *
	 * @param view the view
	 */
	public void clearCache(View view) {
		new ImageLoader(this).clearCache();
		Toast.makeText(this, "Cache cleared", Toast.LENGTH_SHORT).show();
	}
	
	/**
	 * Update cache.
	 *
	 * @param view the view
	 */
	public void updateCache(View view) {
		final Dialog homeSetup = new Dialog(mContext);	
		homeSetup.setContentView(R.layout.dialog_home_setup);
		homeSetup.setTitle("Initial Setup");
		homeSetup.show();

		// handler for the background updating
		final Handler muniHandler = new Handler() {
			TextView status = (TextView) homeSetup.findViewById(R.id.status);
			ProgressBar progress = (ProgressBar) homeSetup.findViewById(R.id.progressBar);
			int mStatus;
			public void handleMessage(Message msg) {
				if (mStatus == Util.PENDING) {
					mStatus = Util.IN_PROGRESS;
					status.setTypeface(null, Typeface.BOLD);
					status.setText("In Progress");
					progress.setProgress(0);
					progress.setMax(msg.arg1);
				} else {
					progress.setProgress(msg.arg2);
					if (msg.arg1 == msg.arg2) {
						mStatus = Util.COMPLETE;
						status.setTypeface(null, Typeface.NORMAL);
						status.setTextColor(Color.GREEN);
						status.setText("Complete");
						homeSetup.dismiss();
					}
				}
			}
		};
		
		GlobalDb db = new GlobalDb(this);
		db.hardReset(muniHandler);
	}
}
